using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TradeFlow.Server.Service;

namespace TradeFlow.Web.Controllers
{
    /// <summary>
    /// 统计报表控制器
    /// </summary>
    [Authorize]
    public class JxReportController : Controller
    {
        private readonly JxReportService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public JxReportController(JxReportService service)
        {
            _service = service;
        }

        #region 页面

        /// <summary>
        /// 进货明细表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult PurchaseDetailReport()
        {
            return View();
        }

        /// <summary>
        /// 进退货明细表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult PurchaseReturnDetailReport()
        {
            return View();
        }

        /// <summary>
        /// 销货明细表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SaleDetailReport()
        { 
            return View();
        }

        /// <summary>
        /// 销退货明细表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SaleReturnDetailReport()
        { 
            return View();
        }

        /// <summary>
        /// 单据明细查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult BillDetail()
        { 
            return View();
        }

        /// <summary>
        /// 利润报表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ProfitReport()
        { 
            return View();
        }

        #endregion

        #region API

        /// <summary>
        /// 查询单据明细
        /// </summary>
        /// <param name="pageNo">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="type">单据类型</param>
        /// <param name="partner">供应商/客户</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> QueryBillDetails(int pageNo = 1, int pageSize = 20, string type = null, string partner = null)
        { 
            var result = await _service.QueryBillDetailsAsync(pageNo, pageSize, type, partner);
            return Json(result);
        }

        /// <summary>
        /// 查询利润报表
        /// </summary>
        /// <param name="pageNo">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> QueryProfits(int pageNo = 1, int pageSize = 20)
        { 
            var result = await _service.QueryProfitsAsync(pageNo, pageSize);
            return Json(result);
        }

        ///// <summary>
        ///// 获取进货明细表
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //    return View();
        //}

        ///// <summary>
        ///// 销退货明细表页面
        ///// </summary>
        ///// <returns></returns>
        //public IActionResult SaleReturnDetailReport()
        //{
        //    return View();
        //}

        /// <summary>
        /// 商品利润表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult GoodsProfitReport()
        {
            return View();
        }

        #endregion

        #region API接口

        /// <summary>
        /// 获取进货明细表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="partnerName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPurchaseDetailReport(DateTime? startDate, DateTime? endDate, string partnerName = "")
        {
            var result = await _service.GetPurchaseDetailReportAsync(startDate, endDate, partnerName);
            return Json(result);
        }

        /// <summary>
        /// 获取进退货明细表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="partnerName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPurchaseReturnDetailReport(DateTime? startDate, DateTime? endDate, string partnerName = "")
        {
            var result = await _service.GetPurchaseReturnDetailReportAsync(startDate, endDate, partnerName);
            return Json(result);
        }

        /// <summary>
        /// 获取销货明细表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="partnerName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSaleDetailReport(DateTime? startDate, DateTime? endDate, string partnerName = "")
        {
            var result = await _service.GetSaleDetailReportAsync(startDate, endDate, partnerName);
            return Json(result);
        }

        /// <summary>
        /// 获取销退货明细表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="partnerName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSaleReturnDetailReport(DateTime? startDate, DateTime? endDate, string partnerName = "")
        {
            var result = await _service.GetSaleReturnDetailReportAsync(startDate, endDate, partnerName);
            return Json(result);
        }

        /// <summary>
        /// 获取商品利润表
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetGoodsProfitReport(DateTime? startDate, DateTime? endDate)
        {
            var result = await _service.GetGoodsProfitReportAsync(startDate, endDate);
            return Json(result);
        }

        #endregion
    }
}